<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-dept-edit" th:object="${Dept}">
        <div class="form-group">
            <label class="col-sm-3 control-label">部门名称：</label>
            <div class="col-sm-8">
                <input class="form-control" type="hidden" name="depId" th:value="*{depId}">
                <input class="form-control" type="text" name="deptName" th:value="*{deptName}">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">负责人：</label>
            <div class="col-sm-8">
                <select id="leader" name="leader" class="form-control select2-hidden-accessible">
                    <option> - -</option>
                    <option th:each="user:${users}" th:value="${user.uid}" th:text="${user.name}"
                            th:field="*{leader}"></option>
                </select>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label">说明：</label>
            <div class="col-sm-8">
                <textarea th:value="*{introduce}" th:text="*{introduce}" name="introduce" class="form-control" rows="6"
                          cols="70"></textarea>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label">状态：</label>
            <div class="col-sm-8">
                <div class="onoffswitch">
                    <input type="checkbox" th:checked="*{status eq 0 ? true : false}" class="onoffswitch-checkbox"
                           id="status" name="status" th:value="*{status}">
                    <label class="onoffswitch-label" for="status">
                        <span class="onoffswitch-inner"></span>
                        <span class="onoffswitch-switch"></span>
                    </label>
                </div>
            </div>
        </div>

        <div class="form-group">
            <div class="form-control-static col-sm-offset-9">
                <button type="submit" class="btn btn-primary">提交</button>
                <button onclick="$.modal.close()" class="btn btn-danger" type="button">关闭</button>
            </div>
        </div>
    </form>
</div>
<div th:include="include::footer"></div>
<script th:src="@{/ajax/libs/select/select2.js}"></script>
<script type="text/javascript">
    var prefix = "/dept"

    $("#form-dept-edit").validate({
        rules: {
            deptName: {
                required: false,
                remote: {
                    url: prefix + "/checkDeptNameUnique",
                    type: "post",
                    dataType: "json",
                    data: {
                        "depId":function () {
                            return $("input[name='depId']").val()
                        },
                        "deptName": function () {
                            var dName = $("input[name='deptName']");
                            return dName.val();
                        }
                    },
                    dataFilter: function (data, type) {
                        if (data == "0") return true;
                        else return false;
                    }
                }
            },
        },
        messages: {
            "deptName": {
                remote: "部门已经存在"
            }
        },
        submitHandler: function (form) {
            $.operate.save(prefix + "/editSave", data());
        }
    });

    function data() {
        var deptName = $("input[name='deptName']").val();
        var depId = $("input[name='depId']").val();
        var leader = $("select[name='leader'] option:selected").val();
        var status = $("input[name='status']").is(':checked') == true ? 0 : 1;
        var text = $("textarea[name='introduce']").val();
        return {
            "deptName": deptName,
            "leader": leader,
            "status": status,
            "introduce": text,
            "depId": depId
        }

    }
</script>
</body>
</html>
